import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/global.dart';
import 'package:yurun_smartmerchants/interface/DialogInterface.dart';
import 'package:yurun_smartmerchants/models/car_add_entity.dart';
import 'package:yurun_smartmerchants/models/car_query_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/net/NetUtil.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/routes/routes.dart';
import 'package:yurun_smartmerchants/utils/DialogUtil.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/evenbus_utils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';
import 'package:yurun_smartmerchants/utils/StringUtils.dart';
import 'package:yurun_smartmerchants/view/EmptyView.dart';

class ApproachChooseCar extends StatefulWidget {
  @override
  _ApproachChooseCarState createState() => _ApproachChooseCarState();
}

class _ApproachChooseCarState extends State<ApproachChooseCar>
    implements DialogInterface {
  var bus = EventBus();
  List<CarQueryContent> content;
  int position; //删除货主编辑的position
  int consumerId;

  @override
  void initState() {
    super.initState();
    String consumer_id = Global.prefs.getString(Global.consumer_id);
    consumerId = int.parse(consumer_id);

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      getList();
    });

    bus.on("addCar", (arg) {
      getList();
    });
  }

  @override
  void dispose() {
    super.dispose();
    bus.off("addCar");
  }

  void getList() {
    LoadingUtil.showLoading(context, tip: '加载中...');
    ApiInterface.queryCar(consumerId).then((value) {
      CarQueryEntity carQueryEntity = CarQueryEntity.fromJson(value);
      setState(() {
        content = carQueryEntity.content;
      });
    }).whenComplete(() => LoadingUtil.hideLoading(context));
  }

  void deleteCar(int id) {
    LoadingUtil.showLoading(context, tip: '加载中...');
    ApiInterface.deleteCar(consumerId, id).then((value) {
      CarAddEntity carAddEntity = CarAddEntity.fromJson(value);
      if (carAddEntity.success) {
        getList();
        bus.send("deleteCar", id);
      }
    }).whenComplete(() => LoadingUtil.hideLoading(context));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('选择车辆',
              style: TextStyle(fontSize: 18, color: Colours.app_words)),
          backgroundColor: Colours.title_bg,
          iconTheme: IconThemeData(color: Colours.app_words),
        ),
        body: Container(
            width: double.infinity,
            height: double.infinity,
            color: Colours.gray_ef,
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Container(
                      width: double.infinity,
                      height: double.infinity,
                      margin: EdgeInsets.only(
                          top: AppSize.sp(20), bottom: AppSize.sp(20)),
                      child: Stack(children: <Widget>[
                        ListView.builder(
                            itemBuilder: (context, index) {
                              return getItem(context, index);
                            },
                            itemCount: content == null ? 0 : content.length),
                       // EmptyView.show(content != null && content.length == 0 ? false : true, 'images/approach_no_car.png', "暂无车辆"),
                        EmptyView(
                            isHide:content != null && content.length == 0 ? false : true,
                            imagePatch: 'images/approach_no_car.png',
                            content:"暂无车辆" ,
                        )
                      ])),
                ),
                GestureDetector(
                    onTap: () {
                      Navigator.pushNamed(context, approach_edit_car);
                    },
                    child: Container(
                        height: AppSize.height(105),
                        color: Colours.white,
                        child: Container(
                            margin: EdgeInsets.all(AppSize.sp(12)),
                            height: AppSize.height(82),
                            alignment: Alignment.center,
                            decoration: new BoxDecoration(
                              color: Colours.app_main,
                              borderRadius: BorderRadius.all(
                                Radius.circular(AppSize.sp(5)),
                              ),
                            ),
                            child: Row(
                              children: <Widget>[
                                Expanded(
                                  flex: 1,
                                  child: Container(
                                      alignment: Alignment.centerRight,
                                      margin: EdgeInsets.only(
                                          right: AppSize.sp(20)),
                                      child: Image.asset(
                                        'images/approach_add.png',
                                        width: AppSize.width(43),
                                        height: AppSize.height(43),
                                      )),
                                ),
                                Expanded(
                                    flex: 1,
                                    child: Container(
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          "新增车辆",
                                          style: TextStyle(
                                              color: Colours.white,
                                              fontSize: AppSize.sp(32)),
                                        )))
                              ],
                            ))))
              ],
            )));
  }

  Widget getItem(BuildContext context, int index) {
    CarQueryContent carQueryContent = content[index];
    return GestureDetector(
      onTap: () {
        Navigator.pop(context);
        bus.send("carType", content[index]);
      },
      child: Container(
        color: Colours.white,
        height: AppSize.height(98),
        alignment: Alignment.center,
        margin: EdgeInsets.only(bottom: AppSize.sp(1)),
        child: Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left: AppSize.sp(32)),
              child: Text(
                  StringUtils.isEmpty(carQueryContent.provinceShortName)?"无车牌(皮重:"+carQueryContent.tare.toString()+"吨)"
                  : carQueryContent.provinceShortName +
                    carQueryContent.licencePlate+"(皮重:"+carQueryContent.tare.toString()+"吨)",
                style: TextStyle(
                    fontSize: AppSize.sp(30), color: Colours.color_313131),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: AppSize.sp(20)),
              decoration: new BoxDecoration(
                color: Colours.app_main,
                borderRadius: BorderRadius.all(
                  Radius.circular(AppSize.sp(4)),
                ),
              ),
              child: Text(
                carQueryContent.typeName,
                style:
                    TextStyle(fontSize: AppSize.sp(22), color: Colours.white),
              ),
            ),
            Spacer(),
            GestureDetector(
                onTap: () {
                  position = index;
                  Navigator.pushNamed(context, approach_edit_car,
                      arguments: content[position]);
                },
                child: Container(
                    margin: EdgeInsets.only(right: AppSize.sp(32)),
                    child: Image.asset(
                      'images/approach_car_edit.png',
                      width: AppSize.width(34),
                      height: AppSize.height(34),
                    ))),
            Container(
              color: Colours.color_dcdcdc,
              height: AppSize.height(32),
              width: AppSize.width(1),
              margin: EdgeInsets.only(right: AppSize.sp(32)),
            ),
            GestureDetector(
                onTap: () {
                  position = index;
                  DialogUtil.d
                      .showAnimation(context, "确认删除车辆", "确认", 200, false, this);
                },
                child: Container(
                    margin: EdgeInsets.only(right: AppSize.sp(32)),
                    child: Image.asset(
                      'images/approach_car_del.png',
                      width: AppSize.width(34),
                      height: AppSize.height(34),
                    )))
          ],
        ),
      ),
    );
  }

  @override
  void sumit() {
    // TODO: implement sumit
    int id = content[position].id;
    deleteCar(id);
  }
}
